package org.wwb.antbox.user.service.user;

import org.ant.commons.commons.enums.EnumTool;
import org.ant.commons.commons.enums.SexEnum;
import org.ant.commons.commons.query.enums.Sort;
import org.ant.commons.commons.security.AuthTool;
import org.ant.commons.models.model.BaseIdModel;
import org.ant.commons.models.page.PageInfo;
import org.ant.commons.models.result.BaseEditResult;
import org.ant.commons.models.result.BasePageResult;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.wwb.antbox.user.data.user.SysUserMapper;
import org.wwb.antbox.user.iservice.user.ISysUserService;
import org.wwb.antbox.user.iservice.userrole.ISysUserRoleService;
import org.wwb.antbox.user.model.user.entity.SysUser;
import org.wwb.antbox.user.model.user.enums.SysUserStatusEnum;
import org.wwb.antbox.user.model.user.model.SysUserAddModel;
import org.wwb.antbox.user.model.user.model.SysUserDelModel;
import org.wwb.antbox.user.model.user.model.SysUserEditModel;
import org.wwb.antbox.user.model.user.model.SysUserPageModel;
import org.wwb.antbox.user.model.user.view.SysUserPageView;
import org.wwb.antbox.user.model.userrole.model.SysUserRoleDelModel;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2019/10/4 20:00
 * @Description: TODO
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysUserServiceImpl extends BaseService<SysUserMapper, SysUser, String> implements ISysUserService {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 系统用户分页查询
     * @param sysUserPageModel 分页查询参数模型
     * @return BasePageResult<SysUserPageView>
     */
    @Override
    public BasePageResult<SysUserPageView> getSysUserPage(SysUserPageModel sysUserPageModel) {
        Query build = Query.build(sysUserPageModel)
                .orderby("modify_time", Sort.DESC)
                .page(sysUserPageModel.getPageIndex(),sysUserPageModel.getPageSize());

        PageInfo<SysUser> sysUserPageInfo = queryPage(build);
        PageInfo<SysUserPageView> sysUserPageViewPageInfo = new SysUserPageView().toPageView(sysUserPageInfo);

        BasePageResult<SysUserPageView> basePageResult = new BasePageResult<>();
        basePageResult.setModel(sysUserPageViewPageInfo);
        basePageResult.setSuccess(true);
        return basePageResult;
    }

    @Override
    public BaseResult<Integer> saveSysUser(SysUserAddModel sysUserAddModel) {
        SysUser sysUser = sysUserAddModel.modelToEntity();
        sysUser.setStatus(SysUserStatusEnum.NORMAL.getCode());
        sysUser.setLoginPwd(AuthTool.md5SysUserPassword("123456",sysUserAddModel.getLoginName()));
        sysUser.setSex(sysUserAddModel.getSex());
        int save = mapper.save(sysUser);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(save);
        baseResult.setSuccess(save == 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> delSysUser(SysUserDelModel sysUserDelModel) {
        /**
         * 1、先删除用户角色关系
         */
        SysUserRoleDelModel sysUserRoleDelModel = new SysUserRoleDelModel();
        sysUserRoleDelModel.setUserIds(sysUserDelModel.getIds());
        sysUserRoleService.delSysUserRole(sysUserRoleDelModel);

        /**
         * 2、再删除用户
         */
        Query query = new Query().in("id",sysUserDelModel.getIds()).ignoreLogicDeleteColumn();
        int delete = mapper.deleteByQuery(query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(delete);
        baseResult.setSuccess(delete >= 1);
        return baseResult;
    }

    /**
     * 根据用户id获取用户信息
     * @param id 用户id
     * @return SysUser
     */
    private SysUser getSysUserById(String id){
        SysUser sysUser = mapper.getById(id);
        return sysUser;
    }

    @Override
    public BaseEditResult doEditSysUser(BaseIdModel<String> baseIdModel) {
        SysUser sysUser = getSysUserById(baseIdModel.getId());
        BaseEditResult baseEditResult = new BaseEditResult();
        baseEditResult.setModel(sysUser);
        baseEditResult.getDicts().put("sex",EnumTool.getEnumMap(SexEnum.class));
        baseEditResult.getDicts().put("status",EnumTool.getEnumMap(SysUserStatusEnum.class));
        baseEditResult.setSuccess(true);
        return baseEditResult;
    }

    @Override
    public BaseResult<Integer> editSysUser(SysUserEditModel sysUserEditModel) {
        SysUser sysUser = sysUserEditModel.modelToEntity();
        int edit = mapper.updateIgnoreNull(sysUser);
        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(edit);
        baseResult.setSuccess(edit >= 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> editSysUserPwd(SysUserDelModel sysUserDelModel) {
        Query query = new Query();
        query.in("id",sysUserDelModel.getIds());

        List<SysUser> userList = mapper.list(query);

        for(SysUser sysUser : userList){

            SysUser newSysUser = new SysUser();
            newSysUser.setId(sysUser.getId());
            newSysUser.setLoginPwd(AuthTool.md5SysUserPassword("123456",sysUser.getLoginName()));
            mapper.updateIgnoreNull(newSysUser);
        }

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(userList.size());
        baseResult.setSuccess(true);
        return baseResult;
    }

    @Override
    public BaseResult<SysUser> getUserById(BaseIdModel<String> baseIdModel) {
        SysUser sysUser = getSysUserById(baseIdModel.getId());

        BaseResult<SysUser> baseResult = new BaseResult<>();
        baseResult.setModel(sysUser);
        baseResult.setSuccess(true);
        return baseResult;
    }
}
